package com.ruoyi.business.service.impl;

import java.util.List;

import com.ruoyi.business.domain.TClassStu;
import com.ruoyi.business.domain.vo.TClassStuVo;
import com.ruoyi.business.mapper.TClassStuMapper;
import com.ruoyi.business.service.ITClassStuService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Validator;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author 最爱喝兽奶
 * @date 2022-08-15
 */
@Service
public class TClassStuServiceImpl implements ITClassStuService
{
    @Autowired
    private TClassStuMapper tClassStuMapper;

    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private ISysConfigService configService;


    @Autowired
    protected Validator validator;

     private static final Logger log = LoggerFactory.getLogger(TClassStuServiceImpl.class);

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public TClassStu selectTClassStuById(Long id)
    {
        return tClassStuMapper.selectTClassStuById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param tClassStu 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<TClassStu> selectTClassStuList(TClassStu tClassStu)
    {
        return tClassStuMapper.selectTClassStuList(tClassStu);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param tClassStu 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertTClassStu(TClassStu tClassStu)
    {
        return tClassStuMapper.insertTClassStu(tClassStu);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param tClassStu 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateTClassStu(TClassStu tClassStu)
    {
        return tClassStuMapper.updateTClassStu(tClassStu);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteTClassStuByIds(Long[] ids)
    {
        return tClassStuMapper.deleteTClassStuByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteTClassStuById(Long id)
    {
        return tClassStuMapper.deleteTClassStuById(id);
    }

    @Override
    public String importStudent(List<TClassStuVo> userList, Boolean isUpdateSupport, String operName) {

        if (StringUtils.isNull(userList) || userList.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        String password = configService.selectConfigByKey("sys.user.initPassword");

        for (TClassStuVo student : userList)
        {
            try
            {
                SysUser userInsert = new SysUser();

                // 验证是否存在这个用户
                SysUser u = userMapper.selectUserByUserName(student.getStudentNumber());
                if (StringUtils.isNull(u))
                {
                    //先插主表
                    BeanValidators.validateWithException(validator, student);
                    userInsert.setUserName(student.getStudentNumber());
                    userInsert.setPassword(SecurityUtils.encryptPassword(password));
                    userInsert.setNickName(student.getStudentName());
                    userInsert.setStatus("0");
                    userInsert.setDelFlag("0");
                    userInsert.setCreateBy(operName);
                    userMapper.insertUser(userInsert);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + userInsert.getUserName() + " 导入成功");

                    //再插附表
                    TClassStu stu = new TClassStu();
                    BeanUtils.copyProperties(student,stu);
                    stu.setStudentId(String.valueOf(userInsert.getUserId()));
                    stu.setUploadId(String.valueOf(SecurityUtils.getUserId()));
                    stu.setUploadUser(SecurityUtils.getUsername());
                    tClassStuMapper.insertTClassStu(stu);
                }
                else if (isUpdateSupport)
                {
                    BeanValidators.validateWithException(validator, student);
                    student.setUpdateBy(operName);
                    userMapper.updateUser(userInsert);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + u.getUserName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、账号 " + u.getUserName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、账号 " + student.getStudentName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();    }
}
